package com.wanhelibs.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wanhe.base.util.ObjectUtil;
import com.wanhe.shoppingcart.bean.CartItem;
import com.wanhe.shoppingcart.manager.ICartItem;
import com.wanhelibs.R;

import java.util.List;

/**
 * Created by XieZaiWei on 2017/8/12.
 * 减套餐子项Adapter
 */

public class ReduceSetMealChildAdapter extends BaseAdapter {

	private LayoutInflater	mInflater;
	private List <CartItem>	mArrCartItem;
	private int				mItemLayout;

	public ReduceSetMealChildAdapter(Context context, List <CartItem> arrCartItem, int itemLayout) {
		mInflater = LayoutInflater.from(context);
		mArrCartItem = arrCartItem;
		mItemLayout = itemLayout;
	}

	@Override
	public int getCount() {
		return mArrCartItem.size();
	}

	@Override
	public ICartItem getItem(int position) {
		return mArrCartItem.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(mItemLayout, null);
			viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		final CartItem cartItem = mArrCartItem.get(position);

		if (ObjectUtil.checkObject(cartItem.getSize())) {
			viewHolder.tv_name
					.setText(cartItem.getName() + "(" + cartItem.getSize().getName() + ")");

		} else if (ObjectUtil.checkObjectList(cartItem.getTags())) {
			viewHolder.tv_name
					.setText(cartItem.getName() + "(" + cartItem.getTags().get(0).getName() + ")");
		} else {
			viewHolder.tv_name.setText(cartItem.getName());
		}

		return convertView;
	}

	private static class ViewHolder {
		public TextView tv_name;
	}
}
